package com.blackbird.jang.model;

import java.util.HashMap;
import java.util.Map;

import com.blackbird.jang.Globals;
import com.blackbird.jang.MainActivity;
import com.blackbird.jang.R;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;

/**
 * 方块
 * 
 * @author jang
 * 
 */
public class Block extends Sprite {

	public static final float WIDTH = Globals.BLOCK_WIDTH
			* MainActivity.SCALE_X;
	public static final float HEIGHT = Globals.BLOCK_HEIGHT
			* MainActivity.SCALE_Y;

	public static final Map<String, Integer> SCORE = new HashMap<String, Integer>();

	// 方块颜色
	public static final String[] COLOR = { "black", "white", "red", "yellow",
			"blue", "purple" };

	// 方块图形，注意必须与颜色数组对应
	public static final int[] ICONS = { R.drawable.black, R.drawable.white,
			R.drawable.red, R.drawable.yellow, R.drawable.blue,
			R.drawable.purple };

	private int[] xy = new int[2];

	static {

		// 棋子得分细到每一类棋子，便于以后针对每一种不同的棋子分别计算分值扩展

		if (SCORE.isEmpty()) {
			SCORE.put("black", new Integer(25));
			SCORE.put("white", new Integer(25));
			SCORE.put("red", new Integer(25));
			SCORE.put("yellow", new Integer(25));
			SCORE.put("blue", new Integer(25));
			SCORE.put("purple", new Integer(25));
		}
	}

	public Block(Texture2D tex) {
		super(tex);
	}

	public static Block make(int index) {
		Block obj = new Block(Texture2D.makePNG(ICONS[index]));
		obj.setName(COLOR[index]);
		obj.setScale(MainActivity.SCALE_X, MainActivity.SCALE_Y);
		System.out.println("方块大小:(" + WIDTH + "," + HEIGHT + ")");
		return obj;
	}

	public void setPositionInt(int x, int y) {
		xy[0] = x;
		xy[1] = y;
	}

	public int getPositionIntX() {
		return xy[0];
	}

	public int getPositionIntY() {
		return xy[1];
	}
	
//	private Action sparkle;
	/**
	 * 方块闪烁
	 */
	public void sparkle() {

//		if (sparkle == null) {
			IntervalAction a = (IntervalAction) ScaleTo.make(0.5f, 0.5f, 0.67f)
					.autoRelease();
			IntervalAction r = (IntervalAction) a.reverse().autoRelease();
			IntervalAction seq = (IntervalAction) Sequence.make(a, r)
					.autoRelease();
			Action sparkle = (Action) RepeatForever.make(seq).autoRelease();
//		}

		this.runAction(sparkle);
	}

	

	public void reset() {
		stopAllActions();
		setScale(MainActivity.SCALE_X, MainActivity.SCALE_Y);
	}

}
